Module:Utilisateur:Eru/Test

Une page de Wikipédia, l'encyclopédie libre.

 Documentation[créer] [purger]
local p = {}

local modules = {
	{ 'Site officiel', 'siteOfficiel',
		{ 'titre' }
	},
	{ 'Bases', 'tout_comme_table',
		{ 'architecture', 'art', 'astronomie', 'audiovisuel', 'bande dessinée', 'géographie', 'jeu', 'littérature', 'mode', 'musique', 'organisation', 'recherche', 'religion', 'santé', 'spectacle', 'sport', 'tourisme', 'transport', 'vie publique', 'vivant' }
	},
	{ 'Dictionnaires', 'main',
		{}
	},
	{ 'Autorité', 'authorityControl',
		{}
	},
}

function p.main( frame )
	local parentArgs = frame:getParent().args
	local results = {}
	local cats = {}

	local function addRow( row, key )
		-- ligne vide
		if not row or row == '' then
			return
		end
		-- uniquement des balises de catégories
		if row:gsub( '%[%[[Cc]atégorie:[^%[%]]+%]%]', '' ) == '' then
			cats[ #cats + 1 ] = row
		else
			results[ #results + 1 ] = { value = row, key = key }
		end
	end

	local wikidata = nil
	for _, name in ipairs{ 'wikidata', 'entity', 'id', 1 } do
		local value = parentArgs[ name ]
		if value then
			if type( name ) == 'number' then
				value = mw.text.trim( value )
			end
			if value ~= '' then
				wikidata = value
				break
			end
		end
	end

	for _, module in ipairs( modules ) do
		local nom = module[ 1 ]
		local argNom = parentArgs[ nom ] or parentArgs[ mw.ustring.lower( nom ) ]
		if argNom ~= '-' then
			local fonction = module[ 2 ]
			local argsName = module[ 3 ]
			local args = { wikidata = wikidata, nocaturl = 1 }
			for _, argName in ipairs( argsName ) do
				if parentArgs[ argName ] then
					args[ argName ] = parentArgs[ argName ]
				end
			end

			local key
			if argNom and argNom:match( '^%-?%d+$' ) then
				-- ordre en paramètre : entre 1 et 9 pour positionner au début, entre -1 et -9 pour positionner à la fin
				key = tonumber( argNom )
			end
			if not key or key == 0 then -- sinon ordre à 10*100 par défaut
				key = 1000
			else
				key = key * 100 -- multiplier par 100 en cas de doublon et pour laisser la place aux différentes bases
				if key < 0 then -- si l'ordre est négatif le mettre à la fin
					key = 2000 - ( -key )
				end
			end

			while results[ key ] do -- si l'ordre existe déjà prendre le suivant
				key = key + 1
			end

			local row = require( 'Module:' .. nom .. '/Bac_à_sable' )[ fonction ]( args )
			if type( row ) == 'table' then
				for _, v in ipairs( row ) do
					while results[ key ] do -- pas de chevauchement des différentes bases
						key = key + 1
					end
					addRow( v, key )
				end
			else
				addRow( row, key )
			end
		end
	end

	if #results > 0 then
		table.sort( results,
			function( c1, c2 )
				return c1.key < c2.key 
			end
		)
		local content = {}
		for _, row in ipairs( results ) do
			content[ #content + 1 ] = row.value
		end
		return frame:extensionTag( 'nowiki' ) .. '\n* ' .. table.concat( content, '\n* ' ) .. table.concat( cats )
	else
		return frame:extensionTag( 'nowiki' ) .. table.concat( cats )
	end
end

return p